import request from '@/utils/request'

// ==================== 图书管理 ====================

// 分页查询图书
export function getBookPage(params) {
  return request({
    url: '/v1/books',
    method: 'get',
    params
  })
}

// 获取图书详情
export function getBookDetail(id) {
  return request({
    url: `/v1/books/${id}`,
    method: 'get'
  })
}

// 创建图书
export function createBook(data) {
  return request({
    url: '/v1/books',
    method: 'post',
    data
  })
}

// 更新图书
export function updateBook(id, data) {
  return request({
    url: `/v1/books/${id}`,
    method: 'put',
    data
  })
}

// 删除图书
export function deleteBook(id) {
  return request({
    url: `/v1/books/${id}`,
    method: 'delete'
  })
}

// 批量导入图书
export function importBooks(data) {
  return request({
    url: '/v1/books/import',
    method: 'post',
    data
  })
}

// 导出图书
export function exportBooks(params) {
  return request({
    url: '/v1/books/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

// 上架图书
export function publishBook(id) {
  return request({
    url: `/v1/books/${id}/publish`,
    method: 'post'
  })
}

// 下架图书
export function unpublishBook(id) {
  return request({
    url: `/v1/books/${id}/unpublish`,
    method: 'post'
  })
}

// 获取图书统计
export function getBookStatistics(id) {
  return request({
    url: `/v1/books/${id}/statistics`,
    method: 'get'
  })
}

// ==================== 借阅管理 ====================

// 分页查询借阅记录
export function getBorrowPage(params) {
  return request({
    url: '/v1/borrows',
    method: 'get',
    params
  })
}

// 获取借阅详情
export function getBorrowDetail(id) {
  return request({
    url: `/v1/borrows/${id}`,
    method: 'get'
  })
}

// 借阅图书
export function borrowBook(data) {
  return request({
    url: '/v1/borrows/borrow',
    method: 'post',
    data
  })
}

// 归还图书
export function returnBook(data) {
  return request({
    url: '/v1/borrows/return',
    method: 'post',
    data
  })
}

// 续借图书
export function renewBook(data) {
  return request({
    url: '/v1/borrows/renew',
    method: 'post',
    data
  })
}

// 批量归还
export function batchReturnBooks(data) {
  return request({
    url: '/v1/borrows/batch-return',
    method: 'post',
    data
  })
}

// 获取逾期记录
export function getOverdueRecords(params) {
  return request({
    url: '/v1/borrows/overdue',
    method: 'get',
    params
  })
}

// 处理逾期
export function processOverdue(id, data) {
  return request({
    url: `/v1/borrows/${id}/process-overdue`,
    method: 'post',
    data
  })
}

// 自动处理逾期记录
export function autoProcessOverdueRecords() {
  return request({
    url: '/v1/borrows/auto-process-overdue',
    method: 'post'
  })
}

// 获取借阅统计
export function getBorrowStatistics(params) {
  return request({
    url: '/v1/borrows/statistics',
    method: 'get',
    params
  })
}

// 导出借阅记录
export function exportBorrowRecords(params) {
  return request({
    url: '/v1/borrows/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

// ==================== 图书分类管理 ====================

// 获取分类列表
export function getLibraryCategoryPage(params) {
  return request({
    url: '/v1/categories',
    method: 'get',
    params
  })
}

// 获取分类树
export function getLibraryCategoryTree(parentId) {
  return request({
    url: '/v1/categories/tree',
    method: 'get',
    params: { parentId }
  })
}

// 获取所有分类树
export function getAllLibraryCategoryTree() {
  return request({
    url: '/v1/categories/tree/all',
    method: 'get'
  })
}

// 获取分类详情
export function getLibraryCategoryDetail(id) {
  return request({
    url: `/v1/categories/${id}`,
    method: 'get'
  })
}

// 创建分类
export function createLibraryCategory(data) {
  return request({
    url: '/v1/categories',
    method: 'post',
    data
  })
}

// 更新分类
export function updateLibraryCategory(id, data) {
  return request({
    url: `/v1/categories/${id}`,
    method: 'put',
    data
  })
}

// 删除分类
export function deleteLibraryCategory(id) {
  return request({
    url: `/v1/categories/${id}`,
    method: 'delete'
  })
}

// 导入分类数据
export function importLibraryCategories(data) {
  return request({
    url: '/v1/categories/import',
    method: 'post',
    data
  })
}

// 导出分类数据
export function exportLibraryCategories(params) {
  return request({
    url: '/v1/categories/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

// ==================== 预约管理 ====================

// 分页查询预约记录
export function getReservationPage(params) {
  return request({
    url: '/v1/reservations',
    method: 'get',
    params
  })
}

// 创建预约
export function createReservation(data) {
  return request({
    url: '/v1/reservations',
    method: 'post',
    data
  })
}

// 取消预约
export function cancelReservation(id, reason) {
  return request({
    url: `/v1/reservations/${id}/cancel`,
    method: 'post',
    params: { reason }
  })
}

// 确认预约
export function confirmReservation(id) {
  return request({
    url: `/v1/reservations/${id}/confirm`,
    method: 'post'
  })
}
